package hk.m4s.lr.repair.test.ui.adapter;

import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import hk.m4s.lr.repair.test.R;
import hk.m4s.lr.repair.test.model.GoodsStyleModel;
import hk.m4s.lr.repair.test.ui.goods.SlectGoodsSignActivity;

/**
 * Created by qi.wang on 2017/3/16.
 */

public class SizeGridsAdapter extends BaseAdapter {
    public List<GoodsStyleModel> list = new ArrayList<>();
    public List<GoodsStyleModel> lists = new ArrayList<>();
    SlectGoodsSignActivity context;
    LayoutInflater inflater;
    public int flag = -1;
    public SizeGridsAdapter(SlectGoodsSignActivity context, List<GoodsStyleModel> data) {
        this.context = context;
        this.list = data;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.list_item_grids, parent, false);
            holder.goods_name = (TextView) convertView.findViewById(R.id.goods_name);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.goods_name.setText(list.get(position).getGoodsStyle());
        holder.goods_name.setTag(position);
        if (list.get(position).getSelect().equals("1")) {
            holder.goods_name.setBackgroundResource(R.drawable.btn_ye_backroud);
            holder.goods_name.setTextColor(Color.parseColor("#ffffff"));
        } else {
            holder.goods_name.setBackgroundResource(R.drawable.btn_gray_corn);
            holder.goods_name.setTextColor(Color.parseColor("#000000"));
        }
        holder.goods_name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int num = (Integer) v.getTag();
                for (int i = 0; i < list.size(); i++) {
                    GoodsStyleModel model = list.get(i);
                    if (i == num) {
                        if (model.getSelect().equals("-1")) {
                            model.setSelect("1");
                            context.update(Integer.parseInt(model.getId()),model.getGoodsStyle());
                        } else {
                            model.setSelect("-1");
                        }
                        list.set(num, model);
                        lists.add(model);
                    } else {
                        list.get(i).setSelect("-1");
                        lists.remove(model);
                    }
                }
                notifyDataSetChanged();

            }
        });
        return convertView;
    }

    public static class ViewHolder {
        TextView goods_name;
    }

}
